/**
 * 初始化详情对话框
 */
var AddInsuranceKindInfoDlg = {
    addInsuranceKindInfoData : {},
    canSubmit : true,
};

/**
 * 清除数据
 */
AddInsuranceKindInfoDlg.clearData = function() {
    this.addInsuranceKindInfoData = {};
}
/**
 * 清除数据
 */
AddInsuranceKindInfoDlg.selectInsuranceKind = function(id) {
    if($('#insuranceKindId'+id).is(':checked')) {
        $("#amountLimit"+id).removeAttr("disabled");
    }else{
        $("#amountLimit"+id).attr("disabled","true");
    }
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
AddInsuranceKindInfoDlg.set = function(key, val) {
    this.addInsuranceKindInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
AddInsuranceKindInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
AddInsuranceKindInfoDlg.close = function() {
    parent.layer.close(window.parent.PolicyPlan.layerIndex);
}

/**
 * 收集数据
 */
AddInsuranceKindInfoDlg.collectData = function() {
    var data =  new Array();
    $('input[type=checkbox]').each(function () {
        var dataT = {};
        dataT.id = $(this).val();
        if($(this).is(':checked')){
            //校验月薪
            if($("#amountType"+dataT.id).val() == 2){
                dataT.salaryTimes = $("#amountLimit"+dataT.id).val();
                if(dataT.salaryTimes < 1 || dataT.salaryTimes > 99){

                }
            }
            dataT.hasSelect = 1;
        }else{
            dataT.hasSelect = 0;
            dataT.salaryTimes = 0;
        }
        data.push(dataT);
    })
    this
    .set('id')
    .set('data',JSON.stringify(data));
}

/**
 * 验证数据
 */
AddInsuranceKindInfoDlg.validate = function () {
    var flag = true;
    $('input[type=checkbox]').each(function () {
        var dataT = {};
        dataT.id = $(this).val();
        if($(this).is(':checked')){
            //校验月薪
            if($("#amountType"+dataT.id).val() == 2){
                dataT.salaryTimes = $("#amountLimit"+dataT.id).val();
                if(dataT.salaryTimes < 1 || dataT.salaryTimes > 99){
                    flag = false;
                    return false;
                }
            }
        }
    })
    return flag
};
/**
 * 提交修改
 */
AddInsuranceKindInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        Feng.alert("月薪倍数填写有误!");
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/policyPlan/updateInsuranceKind", function(data){
        Feng.success("修改成功!");
        window.parent.PolicyPlan.table.refresh();
        AddInsuranceKindInfoDlg.canSubmit = true;
        AddInsuranceKindInfoDlg.close();
    },function(data){
        setTimeout(function () {
            AddInsuranceKindInfoDlg.canSubmit = true;
        },3000);
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.addInsuranceKindInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("addInsuranceKindInfoForm", AddInsuranceKindInfoDlg.validateFields);
});
